package com.esun.payment_api.service.impl;

import com.esun.common.bean.PageRequest;
import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.bean.InvContractDetailBean;
import com.esun.payment_api.domain.InvFormContract;
import com.esun.payment_api.domain.InvFormContractExample;
import com.esun.payment_api.domain.SysDictContract;
import com.esun.payment_api.dto.PInvInsParam;
import com.esun.payment_api.mapper.InvFormContractMapper;
import com.esun.payment_api.mapper.SysDictContractMapper;
import com.esun.payment_api.service.InvFormContractService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("InvFormContractService")
public class InvFormContractServiceImpl implements InvFormContractService {

    @Resource
    InvFormContractMapper invFormContractMapper;

    @Resource
    SysDictContractMapper sysDictContractMapper;

    @Override
    public List<InvFormContract> getListByInvId(Integer invId) {
        //创建Example对象
        InvFormContractExample example = new InvFormContractExample();
        //创建criteria
        InvFormContractExample.Criteria criteria = example.createCriteria();
        //where条件
        criteria.andIsDeletedEqualTo("0");
        criteria.andInvIdEqualTo(invId);
        return invFormContractMapper.selectByExample(example);
    }

    @Override
    public int deleteById(Integer invId, Integer id) {
        return invFormContractMapper.deleteById(invId, id);
    }

    @Override
    public void deleteByIdStr(PInvInsParam param) {
        invFormContractMapper.pInvDelContract(param);
    }

    @Override
    public int insertMultiple2(Integer invId, List<SysDictContract> records) {
        int count = 0;
        for (SysDictContract dictContract : records) {
            InvFormContract record = new InvFormContract();
            record.setCoId(dictContract.getCoId());
            record.setContId(dictContract.getId());
            //当前发票id
            record.setInvId(invId);
            if (invFormContractMapper.insertSelective(record) > 0) {
                count++;
            } else {
                return -1;
            }
        }
        return count;
    }

    @Override
    public void insertMultiple(PInvInsParam param) {
        invFormContractMapper.pInvInsContract(param);
    }

    @Override
    public List<SysDictContract> getContractList(Short coId, Integer invId, Integer cusId, String searchStr, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PageRequest pageRequest = new PageRequest();
        Map<String, String> conditions = new HashMap<>();
        conditions.put("coId", coId.toString());
        conditions.put("invId", invId.toString());
        if (!BlankUtils.isBlank(cusId)) {
            conditions.put("cusId", cusId.toString());
        }
        if (!BlankUtils.isBlank(searchStr)) {
            conditions.put("searchStr", searchStr);
        }
        pageRequest.setConditions(conditions);
        return sysDictContractMapper.getContractList(pageRequest);
    }

    @Override
    public List<InvContractDetailBean> getDetailByInvId(Short coId, Integer invId) {
        return invFormContractMapper.getDetailByInvId(coId, invId);
    }
}
